<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2>用户登录</h2>
      </template>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="login-options">
        <el-link type="primary" @click="$router.push('/register')">注册账号</el-link>
        <el-link type="primary" @click="$router.push('/forget')">忘记密码？</el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/user'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const loginFormRef = ref(null)
const userStore = useUserStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    console.log('开始登录，用户名:', loginForm.username)
    
    const res = await login(loginForm)
    console.log('登录响应:', res)
    
    // 检查登录响应
    if (!res) {
      console.log('登录失败：响应为空')
      throw new Error('登录失败，用户名或密码错误')
    }
    
    // 检查用户状态
    console.log('用户状态:', res.status)
    if (res.status === 0) {
      console.log('账号已被禁用')
      ElMessage.error('账号已被禁用，请联系管理员')
      // 清除用户信息
      userStore.clearUserInfo()
      return
    }
    
    // 检查是否包含必要的用户信息
    if (!res.userId) {
      console.log('登录失败：缺少用户信息')
      throw new Error('登录失败，请检查用户名和密码')
    }
    
    // 设置用户信息
    userStore.setUserInfo(res)
    console.log('登录成功，用户信息:', res)
    ElMessage.success('登录成功')
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
    console.error('错误详情:', error.response?.data)
    
    // 检查是否是账号禁用的情况
    if (error.response?.data?.status === 0) {
      ElMessage.error('账号已被禁用，请联系管理员')
    } else if (error.response?.data?.message) {
      // 使用后端返回的具体错误消息
      ElMessage.error(error.response.data.message)
    } else {
      // 如果是用户名或密码错误的情况
      ElMessage.error('登录失败，用户名或密码错误')
    }
    
    // 清除用户信息
    userStore.clearUserInfo()
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
  position: relative;
}

.login-card:hover {
  transform: translateY(-5px);
}

:deep(.el-card__header) {
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
  background: linear-gradient(135deg, #409EFF 0%, #3a8ee6 100%);
  border-radius: 12px 12px 0 0;
}

:deep(.el-card__header h2) {
  margin: 0;
  color: white;
  text-align: center;
  font-size: 24px;
  font-weight: 500;
}

:deep(.el-card__body) {
  padding: 30px;
  padding-bottom: 60px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.1);
}

:deep(.el-button--primary) {
  height: 44px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, #409EFF 0%, #3a8ee6 100%);
  border: none;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

:deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

.login-options {
  margin-top: 40px;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}

:deep(.el-link) {
  font-size: 14px;
}
</style> 